package com.yudean.esb.domain.erp;

import com.yudean.esb.domain.esb.EsbRecvServiceTempData;
import jodd.vtor.Check;
import jodd.vtor.ValidationContext;
import jodd.vtor.Vtor;
import jodd.vtor.constraint.NotNullConstraint;
import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.TypeMappingOptions;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import static org.dozer.loader.api.FieldsMappingOptions.hintA;
import static org.dozer.loader.api.FieldsMappingOptions.hintB;

/**
 * 法务系统专用的付款模型
 *
 * @author 890117
 */
public class ErpFwsPayments implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = -4406070276008701689L;

    private Long controlId;

    private BigDecimal paymentIntId;

    private String compCode;

    private String contractCode;

    private Integer paymentPhase;

    private String paymentType;

    private Date paymentApprovedDate;
    private String paymentApprovedDateStr;

    private BigDecimal amountPayable;

    private BigDecimal amountInvoiced;

    private BigDecimal amountTax;

    private BigDecimal amountDeposit;

    private Date creationDate;
    private String creationDateStr;

    private Integer createdBy;

    private Date lastUpdateDate;
    private String lastUpdateDateStr;

    private Integer lastUpdatedBy;

    private Date erpPaidDate;
    private String erpPaidDateStr;

    private BigDecimal erpPaidAmount;

    private BigDecimal totalPaidAmount;

    private String faFlag;

    private String vendorCode;

    private String vendorName;

    private String historicalContract;

    private Integer docNumber;

    private BigDecimal totalReceiptAmount;

    private BigDecimal vendorTotalPaidAmt;

    private BigDecimal vendorTotalReceiptAmt;

    private String attribute1;

    private String attribute2;

    private String attribute3;

    private String attribute4;

    private String attribute5;

    private String attribute6;

    private String attribute7;

    private String attribute8;

    private String attribute9;

    private String attribute10;

    public long getControlId() {
        return controlId;
    }

    public void setControlId(long controlId) {
        this.controlId = controlId;
    }

    public BigDecimal getPaymentIntId() {
        return paymentIntId;
    }

    public void setPaymentIntId(BigDecimal paymentIntId) {
        this.paymentIntId = paymentIntId;
    }

    public String getCompCode() {
        return compCode;
    }

    public void setCompCode(String compCode) {
        this.compCode = compCode;
    }

    public String getContractCode() {
        return contractCode;
    }

    public void setContractCode(String contractCode) {
        this.contractCode = contractCode;
    }

    public Integer getPaymentPhase() {
        return paymentPhase;
    }

    public void setPaymentPhase(Integer paymentPhase) {
        this.paymentPhase = paymentPhase;
    }

    public String getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }

    public Date getPaymentApprovedDate() {
        return paymentApprovedDate;
    }

    public void setPaymentApprovedDate(Date paymentApprovedDate) {
        this.paymentApprovedDate = paymentApprovedDate;
    }

    public String getPaymentApprovedDateStr() {
        return paymentApprovedDateStr;
    }

    public void setPaymentApprovedDateStr(String paymentApprovedDateStr) {
        this.paymentApprovedDateStr = paymentApprovedDateStr;
        this.paymentApprovedDate = ErpControl.longDateParser(paymentApprovedDateStr);
    }

    public BigDecimal getAmountPayable() {
        return amountPayable;
    }

    public void setAmountPayable(BigDecimal amountPayable) {
        this.amountPayable = amountPayable;
    }

    public BigDecimal getAmountInvoiced() {
        return amountInvoiced;
    }

    public void setAmountInvoiced(BigDecimal amountInvoiced) {
        this.amountInvoiced = amountInvoiced;
    }

    public BigDecimal getAmountTax() {
        return amountTax;
    }

    public void setAmountTax(BigDecimal amountTax) {
        this.amountTax = amountTax;
    }

    public BigDecimal getAmountDeposit() {
        return amountDeposit;
    }

    public void setAmountDeposit(BigDecimal amountDeposit) {
        this.amountDeposit = amountDeposit;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getCreationDateStr() {
        return creationDateStr;
    }

    public void setCreationDateStr(String creationDateStr) {
        this.creationDateStr = creationDateStr;
        this.creationDate = ErpControl.longDateParser(creationDateStr);
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Date getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Date lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public String getLastUpdateDateStr() {
        return lastUpdateDateStr;
    }

    public void setLastUpdateDateStr(String lastUpdateDateStr) {
        this.lastUpdateDateStr = lastUpdateDateStr;
        this.lastUpdateDate = ErpControl.longDateParser(lastUpdateDateStr);
    }

    public Integer getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(Integer lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Date getErpPaidDate() {
        return erpPaidDate;
    }

    public void setErpPaidDate(Date erpPaidDate) {
        this.erpPaidDate = erpPaidDate;
    }

    public String getErpPaidDateStr() {
        return erpPaidDateStr;
    }

    public void setErpPaidDateStr(String erpPaidDateStr) {
        this.erpPaidDateStr = erpPaidDateStr;
        this.erpPaidDate = ErpControl.longDateParser(erpPaidDateStr);
    }

    public BigDecimal getErpPaidAmount() {
        return erpPaidAmount;
    }

    public void setErpPaidAmount(BigDecimal erpPaidAmount) {
        this.erpPaidAmount = erpPaidAmount;
    }

    public BigDecimal getTotalPaidAmount() {
        return totalPaidAmount;
    }

    public void setTotalPaidAmount(BigDecimal totalPaidAmount) {
        this.totalPaidAmount = totalPaidAmount;
    }

    public String getFaFlag() {
        return faFlag;
    }

    public void setFaFlag(String faFlag) {
        this.faFlag = faFlag;
    }

    public String getVendorCode() {
        return vendorCode;
    }

    public void setVendorCode(String vendorCode) {
        this.vendorCode = vendorCode;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public String getHistoricalContract() {
        return historicalContract;
    }

    public void setHistoricalContract(String historicalContract) {
        this.historicalContract = historicalContract;
    }

    public Integer getDocNumber() {
        return docNumber;
    }

    public void setDocNumber(Integer docNumber) {
        this.docNumber = docNumber;
    }

    public BigDecimal getTotalReceiptAmount() {
        return totalReceiptAmount;
    }

    public void setTotalReceiptAmount(BigDecimal totalReceiptAmount) {
        this.totalReceiptAmount = totalReceiptAmount;
    }

    public BigDecimal getVendorTotalPaidAmt() {
        return vendorTotalPaidAmt;
    }

    public void setVendorTotalPaidAmt(BigDecimal vendorTotalPaidAmt) {
        this.vendorTotalPaidAmt = vendorTotalPaidAmt;
    }

    public BigDecimal getVendorTotalReceiptAmt() {
        return vendorTotalReceiptAmt;
    }

    public void setVendorTotalReceiptAmt(BigDecimal vendorTotalReceiptAmt) {
        this.vendorTotalReceiptAmt = vendorTotalReceiptAmt;
    }

    public String getAttribute1() {
        return attribute1;
    }

    public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
    }

    public String getAttribute2() {
        return attribute2;
    }

    public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
    }

    public String getAttribute3() {
        return attribute3;
    }

    public void setAttribute3(String attribute3) {
        this.attribute3 = attribute3;
    }

    public String getAttribute4() {
        return attribute4;
    }

    public void setAttribute4(String attribute4) {
        this.attribute4 = attribute4;
    }

    public String getAttribute5() {
        return attribute5;
    }

    public void setAttribute5(String attribute5) {
        this.attribute5 = attribute5;
    }

    public String getAttribute6() {
        return attribute6;
    }

    public void setAttribute6(String attribute6) {
        this.attribute6 = attribute6;
    }

    public String getAttribute7() {
        return attribute7;
    }

    public void setAttribute7(String attribute7) {
        this.attribute7 = attribute7;
    }

    public String getAttribute8() {
        return attribute8;
    }

    public void setAttribute8(String attribute8) {
        this.attribute8 = attribute8;
    }

    public String getAttribute9() {
        return attribute9;
    }

    public void setAttribute9(String attribute9) {
        this.attribute9 = attribute9;
    }

    public String getAttribute10() {
        return attribute10;
    }

    public void setAttribute10(String attribute10) {
        this.attribute10 = attribute10;

    }

    /**
     * 判断是否有不满足的约束的地方
     *
     * @return TRUE 有不满足要求的字段信息，FALSE 数据符合规范
     */
    public boolean hasViolations() {
        if (validationContext == null)
            validationContext = getValidationContext();
        Vtor vtor = new Vtor();
        vtor.validate(validationContext, this);
        return vtor.hasViolations();
    }

    /**
     * 用来构造验证字段的上下文
     *
     * @return 验证规范
     */
    private static ValidationContext getValidationContext() {
        ValidationContext vctx = new ValidationContext();
        vctx.add(new Check("controlId", new NotNullConstraint()));
        vctx.add(new Check("paymentIntId", new NotNullConstraint()));
        vctx.add(new Check("compCode", new NotNullConstraint()));
        vctx.add(new Check("contractCode", new NotNullConstraint()));
        vctx.add(new Check("paymentPhase", new NotNullConstraint()));
        vctx.add(new Check("paymentType", new NotNullConstraint()));
        vctx.add(new Check("paymentApprovedDate", new NotNullConstraint()));
        vctx.add(new Check("amountPayable", new NotNullConstraint()));
        vctx.add(new Check("amountInvoiced", new NotNullConstraint()));
        return vctx;
    }

    // 验证的条件
    private static ValidationContext validationContext = null;
    private static DozerBeanMapper mapper;
    //付款信息的映射关系
    public static BeanMappingBuilder paymentsMapping = new BeanMappingBuilder() {
        @Override
        protected void configure() {
            //对于日期的转换
            String dateFormat = "yyyy-MM-dd HH:mm:ss";
            mapping(Map.class, ErpFwsPayments.class,
                    TypeMappingOptions.dateFormat(dateFormat))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE1"), "controlId", hintA(String.class), hintB(Long.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE2"), "paymentIntId", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE3"), "compCode")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE4"), "contractCode")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE5"), "paymentPhase", hintA(String.class), hintB(Integer.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE6"), "paymentType")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE7"), "paymentApprovedDate", hintA(String.class), hintB(Date.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE8"), "amountPayable", hintB(BigDecimal.class), hintA(String.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE9"), "amountInvoiced", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE10"), "amountTax", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE11"), "amountDeposit", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE12"), "creationDate", hintA(String.class), hintB(Date.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE13"), "createdBy", hintA(String.class), hintB(Integer.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE14"), "lastUpdateDate", hintA(String.class), hintB(Date.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE15"), "lastUpdatedBy", hintA(String.class), hintB(Integer.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE16"), "erpPaidDate", hintA(String.class), hintB(Date.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE17"), "erpPaidAmount", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE18"), "totalReceiptAmount", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE19"), "faFlag")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE20"), "vendorCode")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE21"), "vendorName")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE22"), "historicalContract")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE23"), "docNumber", hintA(String.class), hintB(Integer.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE24"), "totalReceiptAmount", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE25"), "vendorTotalPaidAmt", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE26"), "vendorTotalReceiptAmt", hintA(String.class), hintB(BigDecimal.class))
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE31"), "attribute1")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE32"), "attribute2")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE33"), "attribute3")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE34"), "attribute4")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE35"), "attribute5")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE36"), "attribute6")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE37"), "attribute7")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE38"), "attribute8")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE39"), "attribute9")
                    .fields(ErpControl.getMapperDefine("ATTRIBUTE40"), "attribute10")
            ;
        }
    };

    static {
        mapper = new DozerBeanMapper();
        mapper.addMapping(paymentsMapping);
    }

    /**
     * 转换成临时数据存储对象
     *
     * @return 临时对象
     */
    public EsbRecvServiceTempData convert2TmpData() {
        Map<String, Object> data = mapper.map(this, Map.class);
        data.put("ATTRIBUTE99", "FWS-TMP-DATA");

        EsbRecvServiceTempData tmpData = new EsbRecvServiceTempData();
        tmpData.setIsProcessed("1");
        tmpData.setID("FWS-ERP-PAYMENT");
        tmpData.setPackageID(this.getControlId() + "");
        tmpData.setOperation("T");
        tmpData.setTransactionID(this.getContractCode());

        ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        list.add(data);
        tmpData.setData(list);
        return tmpData;
    }

    /**
     * 将临时表的信息转存为合同数据
     *
     * @param tmpData 临时表
     * @return 合同数据集合
     */
    public static ErpFwsPayments mapToThis(Map<String, Object> tmpData) {
        if (tmpData == null) {
            return null;
        }
        return mapper.map(tmpData, ErpFwsPayments.class);
    }

    @Override
    public String toString() {
        return "ErpFwsPayments{" + "controlId=" + controlId + ", paymentIntId="
                + paymentIntId + ", compCode='" + compCode + '\''
                + ", contractCode='" + contractCode + '\'' + ", paymentPhase="
                + paymentPhase + ", paymentType='" + paymentType + '\''
                + ", paymentApprovedDate=" + paymentApprovedDate
                + ", paymentApprovedDateStr='" + paymentApprovedDateStr + '\''
                + ", amountPayable=" + amountPayable + ", amountInvoiced="
                + amountInvoiced + ", amountTax=" + amountTax
                + ", amountDeposit=" + amountDeposit + ", creationDate="
                + creationDate + ", creationDateStr='" + creationDateStr + '\''
                + ", createdBy=" + createdBy + ", lastUpdateDate="
                + lastUpdateDate + ", lastUpdateDateStr='" + lastUpdateDateStr
                + '\'' + ", lastUpdatedBy=" + lastUpdatedBy + ", erpPaidDate="
                + erpPaidDate + ", erpPaidDateStr='" + erpPaidDateStr + '\''
                + ", erpPaidAmount=" + erpPaidAmount + ", totalPaidAmount="
                + totalPaidAmount + ", faFlag='" + faFlag + '\''
                + ", vendorCode='" + vendorCode + '\'' + ", vendorName='"
                + vendorName + '\'' + ", historicalContract='"
                + historicalContract + '\'' + ", docNumber=" + docNumber
                + ", totalReceiptAmount=" + totalReceiptAmount
                + ", vendorTotalPaidAmt=" + vendorTotalPaidAmt
                + ", vendorTotalReceiptAmt=" + vendorTotalReceiptAmt
                + ", attribute1='" + attribute1 + '\'' + ", attribute2='"
                + attribute2 + '\'' + ", attribute3='" + attribute3 + '\''
                + ", attribute4='" + attribute4 + '\'' + ", attribute5='"
                + attribute5 + '\'' + ", attribute6='" + attribute6 + '\''
                + ", attribute7='" + attribute7 + '\'' + ", attribute8='"
                + attribute8 + '\'' + ", attribute9='" + attribute9 + '\''
                + ", attribute10='" + attribute10 + '\'' + '}';
    }
}
